package org.darkforest.ucenter.auth;

import org.darkforest.common.util.CookieUtils;
import org.darkforest.common.util.JSONUtils;
import org.darkforest.vo.MemberBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by valentina on 15/3/6.
 * wap及微信端用户是否登录权限拦截
 */
public class AuthByCookiesInterceptor extends HandlerInterceptorAdapter {

    private Logger logger = LoggerFactory.getLogger(AuthByCookiesInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return super.preHandle(request, response, handler);
        String url = request.getServletPath();
        Map params = request.getParameterMap();

        String paramStr = "";
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            AuthPassport authPassport = ((HandlerMethod) handler).getMethodAnnotation(AuthPassport.class);

            //没有声明需要权限,或者声明不验证权限
            if (authPassport == null || authPassport.isLogin() == true) {
                return true;
            } else {
                logger.debug("-check  wxauth---");

                MemberBase member = (MemberBase) request.getSession().getAttribute("memInfo");
//                member = new MemberBase();
//                member.setUserId(10130);
//                member.setWxOpenId("o9ICkwWBwGiszP8DMk-pdjwNiZY8");
//                member.setUserName("test");
//                request.getSession().setAttribute("memInfo", member);

                if (member == null) {
                    if (CookieUtils.getCookieByName(request, "memInfo") == null) {
                        logger.info("cookie中存放的用户数据"+CookieUtils.getCookieByName(request, "memInfo"));
                        member = JSONUtils.toBean(CookieUtils.getCookieByName(request, "memInfo"), MemberBase.class);
                        if(member!=null){
                            logger.info("memberbase信息"+member.toString());
                            request.getSession().setAttribute("memInfo",member);
                        }else{
                            member=null;
                        }
                    }
                }

                //获取参数集合并写入到reurl中
                if (params != null && params.size() > 0) {
                    Iterator it = params.keySet().iterator();
                    int idx = 0;
                    while (it.hasNext()) {
                        String key = it.next().toString();
                        String value = request.getParameter(key);
                        paramStr += (idx == 0 ? "?" : "&") + key + "=" + value;
                        idx++;
                    }
                }
                logger.debug(url + paramStr);
                if (member == null) {
                    response.sendRedirect("http://zaihu.bayss.com/wechat/wxPay/getAuthorize?type=2&reUrl=" + url + paramStr);
                    return false;
                } else {
                    return true;
                }

            }
        } else {
            return true;

        }
    }

}
